//
//  RegisterDoctorController.m
//  HarmonizePub
//
//  Created by Zy on 16/9/21.
//  Copyright © 2016年 Zy. All rights reserved.
//


/**
 * 你好！我是Zy，这个项目头号挖坑人.
 * 如果，你是按照流程来到这里的话.
 * 那么，恭喜你，你已经离看完了这个功能不远了.
 * 但是，如果你以为这个功能只是如此的话.
 * 那么，你离换工作不远了...
 */


#import "RegisterDoctorController.h"
#import <YYModel.h>
#import "Config.h"
#import "ScheduleDoctor.h"
#import "ChoosePatientViewController.h"
#import "RegisterCollectionViewCell.h"
#import "NoCardAppointmentViewViewController.h"

static NSString *CellIdentifier = @"Cell";
CGFloat const count = 3;

@interface RegisterDoctorController () <UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSMutableArray *_dataArrayM;
    NSInteger _curItem;
    NSDictionary *_selectDataDict;
    NSDictionary*_dataDic;
    NSString*_ghksdm;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *namelabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionHeightLayoutConstraint;
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (weak, nonatomic) IBOutlet UIButton *noCarfButton;
@end

@implementation RegisterDoctorController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"预约时间";
    _curItem = -1;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemSize = XHScreenWidth / count;
    flowLayout.itemSize = CGSizeMake(itemSize, [RegisterCollectionViewCell itemHeight]);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    
    UINib *nib = [UINib nibWithNibName:[RegisterCollectionViewCell registerName] bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
//    self.button.enabled = NO;
    [self requestPbDetail];
}

#pragma mark --- 获取排班详情 ---
- (void)requestPbDetail
{
    NSString * date = self.dataDict[@"jzrq"];
    NSDictionary *cooike = @{CookieVaule : [UserDefaultsTool stringForKey: XHToken]};
    NSString* valueStr = self.selectDay;
//    if (valueStr != nil ) {
//        NSLog(@"%@",valueStr);
//        NSString * date1 = [valueStr substringFromIndex:4];
//        date = [date1 substringToIndex:date1.length-1];
//    }
    NSDictionary *params = @{
                             @"ksdm" : self.dataDict[@"ksdm"],
                             @"ygdm" : self.dataDict[@"ygdm"],
                             @"date" : date,
                             @"sdbz":self.dataDict[@"sdbz"]
                             };
    [HTTPSTool POSTRequestWithPath: XHPbDetail cookie: cooike params: params success:^(NSDictionary * _Nonnull JSON) {
//        LRLog(@"排班详情%@", JSON);
        _dataArrayM = JSON[@"data"][@"list"];
        [self.collectionView reloadData];
        [self setViewAttr:JSON[@"data"][@"info"]];
        _dataDic = JSON[@"data"][@"info"];
        _ghksdm = JSON[@"data"][@"ghksdm"];
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.description);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)setDataDict:(NSDictionary *)dataDict {
//    _dataDict = dataDict;
//    if (!_dataArrayM) _dataArrayM = [NSMutableArray array];
//    for (NSDictionary *pbListDict in dataDict[@"pbList"])
//        for (NSDictionary *pbDict in pbListDict[@"pb"])
//             [_dataArrayM addObject:pbDict];
//}

- (void)setViewAttr:(NSDictionary *)docDict {
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.layer.cornerRadius = 5.0f;
    self.imageView.layer.masksToBounds = YES;
    NSString *imageString = docDict[@"image"];
    [self.imageView downloadImageWithURLString:imageString placeholderString:@"DoctorDefault.png"];
    self.namelabel.text = docDict[@"ygxm"];
    self.detailLabel.text = docDict[@"description"];
    
    NSInteger column = ceilf(_dataArrayM.count / count);
    CGFloat height = column * [RegisterCollectionViewCell itemHeight];
    if (iPhoneSE) {
        if (height > 200) height = 150;
    } else if (iPhone6s) {
        if (height > 290) height = 240;
    } else if (iPhone6P){
        if (height > 370) height = 320;
    } else if (iPhoneX){
        if (height > 370) height = 320;
    }
    self.collectionHeightLayoutConstraint.constant = height;
    self.button.layer.cornerRadius = 5.0f;
    self.button.layer.masksToBounds = YES;
    self.noCarfButton.layer.cornerRadius = 5.0f;
    self.noCarfButton.layer.masksToBounds = YES;
}


#pragma mark - 马上预约
- (IBAction)appointment:(UIButton *)sender {
    if (!_selectDataDict)
        _selectDataDict = _dataArrayM.firstObject;
    NSMutableDictionary *dictM = [_selectDataDict mutableCopy];
    [dictM setObject:self.depName forKey:@"depName"];
    [dictM setObject:self.namelabel.text forKey:@"ygxm"];
    NSString* valueStr = self.selectDay;
    NSString * date = [valueStr substringFromIndex:4];
    [dictM setObject:date forKey:@"jzrq"];
    NSString * time = dictM[@"time"];
    [dictM setObject:time forKey:@"jzsj"];
    [dictM setObject:_dataDic[@"ygdm"] forKey:@"ygdm"];
    [dictM setObject:_dataDic[@"ksdm"] forKey:@"ksdm"];
    [dictM setObject:_ghksdm forKey:@"ghksdm"];
    [ScheduleDoctor yy_modelWithDictionary:dictM];
    [ChoosePatientViewController patientWithCurrentController:self nextController:@"AppointmentDetailController" patientListType:XHPatientListTypeMedical];
}
#pragma mark - 无卡预约
- (IBAction)noCardAppointment:(UIButton *)sender {
    if (!_selectDataDict)
       _selectDataDict = _dataArrayM.firstObject;
    NSMutableDictionary *dictM = [_selectDataDict mutableCopy];
    [dictM setObject:self.depName forKey:@"depName"];
    [ScheduleDoctor yy_modelWithDictionary:dictM];
    NoCardAppointmentViewViewController *noCardVc = [[NoCardAppointmentViewViewController alloc] init];
    [self.navigationController pushViewController: noCardVc animated: YES];
}

#pragma mark - collectionView delegate/dataSource Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArrayM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RegisterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _dataArrayM[indexPath.item];
    if (_curItem == -1 && [dict[@"pblsh"] isEqualToString:self.selectDay])
        _curItem = indexPath.item;
    cell.isSelectState = indexPath.item == _curItem;
    [cell setCellShowDataWithDataDictionary:dict];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView reloadData];
    NSDictionary  *dateDict = _dataArrayM[indexPath.row];
    _curItem = indexPath.item;
//    NSArray *pbList = self.dataDict[@"pbList"];
//    NSDictionary *dataDict = pbList.lastObject;
//    NSArray *array = dataDict[@"pb"];
//    _selectDataDict = array[_curItem];
    _selectDataDict = dateDict;
}

@end
